package src.Client;
import src.common.MessageType;
import src.common.impl.Message;

import javax. swing. *;
import java. awt .*;
import java.awt.event.*;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.HashMap;

public class FriendList extends JFrame implements WindowListener,ActionListener, MouseListener {
    public static HashMap<String ,FriendChat> hmFriendChat = new HashMap<>();
    //定义卡片1(好友面板)中的组件
    JPanel friendPanel;//卡片1中组件的容器
    JButton myFriendButton1;
    JButton myStrangerButton1;
    JButton blackListButton1;
    JScrollPane friendListScrollPane;//好友列表的滚动条面板
    JPanel friendListPanel;//好友列表面板
    final int MYFRIENDCOUNT=10;//50个好友
    JLabel friendLabel[]=new JLabel[MYFRIENDCOUNT];//定义好友图标数组

    //定义卡片2(陌生人面板)中的组件
    JPanel strangerPanel;//卡片2中组件的容器
    JButton myFriendButton2;
    JButton myStrangerButton2;
    JButton blackListButton2;
    JScrollPane strangerListScrollPane;//陌生人的滚动条面板

    JPanel strangerListPanel;//陌生人列表面板
    final int STRANGERCOUNT=5;//20个陌生人
    JLabel strangerLabel[]=new JLabel [STRANGERCOUNT];

    CardLayout c1;
    String name;

    JPanel addFriendPanel;
    JButton addFriendButton;

    public FriendList (String name,String allFriends) {
        this.name = name;
//1、创建卡片1(好友面板)中的组件
        friendPanel=new JPanel(new BorderLayout());//卡片1设置边界布局模

        addFriendPanel = new JPanel(new GridLayout(2,1));
        addFriendButton = new JButton("添加好友");
        addFriendButton.addActionListener(this);

        myFriendButton1=new JButton("我的好友");
        addFriendPanel.add(addFriendButton);

        addFriendPanel.add(myFriendButton1);

        friendPanel.add(addFriendPanel,"North");

        friendListPanel = new JPanel();
        showAllFriends(allFriends);


        friendListScrollPane=new JScrollPane(friendListPanel);//创建好

        friendPanel.add(friendListScrollPane,"Center");//好友滚动条添加

        myStrangerButton1=new JButton("陌生人");
        myStrangerButton1.addActionListener(this);

        blackListButton1=new JButton("黑名单");
        JPanel stranger_BlackPanel=new JPanel (new GridLayout (2,1)) ;//2
        stranger_BlackPanel.add (myStrangerButton1);
        stranger_BlackPanel.add (blackListButton1);
        friendPanel.add(stranger_BlackPanel,"South");//添加到卡片1的南部
        //2、创建卡片2(陌生人面板)中的组件
        strangerPanel=new JPanel (new BorderLayout ()) ;

        myFriendButton2=new JButton("我的好友");
        myFriendButton2.addActionListener(this);


        myStrangerButton2=new JButton("陌生人");
//为了容纳myFriendButton2和myStrangerButton2定义一个新JPanel面板
        JPanel friend_strangerPanel=new JPanel (new GridLayout (2,1));//2
        friend_strangerPanel.add (myFriendButton2);
        friend_strangerPanel.add (myStrangerButton2);
        strangerPanel.add(friend_strangerPanel,"North");//添加到卡片2的



//创建中间的陌生人列表滚动条面板
        strangerListPanel=new JPanel(new GridLayout (STRANGERCOUNT, 1));
        for(int i=0;i<strangerLabel.length;i++) {
            strangerLabel[i] = new JLabel(i + "号陌生人", new ImageIcon("src/images/tortoise.gif"), JLabel.LEFT);
            strangerListPanel.add(strangerLabel[i]);
        }

        strangerListScrollPane=new JScrollPane (strangerListPanel);
        strangerPanel.add(strangerListScrollPane,"Center");//陌生人滚动

        blackListButton2=new JButton("黑名单");
        strangerPanel.add(blackListButton2,"South");//添加到卡片2的南部

//3、创建卡片布局
        c1=new CardLayout () ;
        this.setLayout(c1);//窗体设置为卡片布局
        this.add(friendPanel,"card1");//窗体中添加卡片
        this.add (strangerPanel, "card2");
        //c1.show(this.getContentPane(),"card1");//在窗体中显示卡片1,默认

        c1.show(this.getContentPane(),"card2");//在窗体中显示卡片2

        this.setIconImage (new ImageIcon ("src/images/duck2.gif").getImage());
        this.setTitle(this.name+"的好友列表");
        this.addWindowListener(this);
//this. setLocationRelativeTo (null) ;
        this.setBounds (800, 600,350, 350);
        this. setVisible (true) ;
    }


    public void showAllFriends(String allFriends) {
        String[] myFriends=allFriends.split(" ");
        friendListPanel.removeAll();
        friendListPanel.setLayout(new GridLayout(myFriends.length-1,1));
        for (int i = 1; i < myFriends.length; i++) {
            String imageStr = "src/images/" + i%6 + ".jpg"; //
            ImageIcon imageIcon = new ImageIcon(imageStr);

            friendLabel[i] = new JLabel(myFriends[i] + "", imageIcon, JLabel.LEFT);
            friendLabel[i].addMouseListener(this);
            friendListPanel.add(friendLabel[i]);//好友图标添加到好友列表面
        }
        friendListPanel.revalidate();
    }

    public void actionPerformed(ActionEvent ee) {
        if (ee.getSource() == myFriendButton2){
            c1.show(this.getContentPane(),"card1");
        }
        if (ee.getSource() == myStrangerButton1){
            c1.show(this.getContentPane(),"card2");
        }
        if (ee.getSource() == addFriendButton){
            String newFriend = JOptionPane.showInputDialog("请输入新好友的名字: ");
            if (newFriend != null){
                System.out.println("新好友"+newFriend);
                Message message = new Message();
                message.setSender(name);
                message.setReceiver("Server");
                message.setContent(newFriend);
                message.setMessageType(MessageType.ADD_NEW_FRIEND);
                try {
                    ObjectOutputStream oos = new ObjectOutputStream(YYChatClientConnection.s.getOutputStream());
                    oos.writeObject(message);
                }catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if (e.getClickCount() == 2){
            JLabel jl = (JLabel) e.getSource();
            String toName = jl.getText();
            FriendChat fc = new FriendChat(name, toName);
            hmFriendChat.put(name+"对"+toName, fc);
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        JLabel jl = (JLabel) e.getSource();
        jl.setForeground(Color.BLUE);
    }

    @Override
    public void mouseExited(MouseEvent e) {
        JLabel jl = (JLabel) e.getSource();
        jl.setForeground(Color.RED);
    }

    public void activeOnlineFriendIcon(Message message){
        String onlineFriend = message.getContent();
        String[] onlineFriendName = onlineFriend.split(" ");
        for (String s : onlineFriendName) {
            //this.friendLabel[Integer.valueOf(s)].setEnabled(true);
        }
    }

    public void activeNewOnlineFriendIcon(String newOnlineFriend){
        //this.friendLabel[Integer.valueOf(newOnlineFriend)].setEnabled(true);
    }


    @Override
    public void windowOpened(WindowEvent e) {

    }

    @Override
    public void windowClosing(WindowEvent e) {
        System.out.println(name+"准备关闭服务端.....");
        Message message = new Message();
        message.setSender(name);
        message.setReceiver("Server");
        message.setMessageType(MessageType.USER_EXIT_SERVER_THREAD_CLOSE);
        ObjectOutputStream oos;
        try {
            oos = new ObjectOutputStream(YYChatClientConnection.s.getOutputStream());
            oos.writeObject(message);
        }catch (IOException ee){
            ee.printStackTrace();
        }

        System.out.println(0);
    }

    @Override
    public void windowClosed(WindowEvent e) {

    }

    @Override
    public void windowIconified(WindowEvent e) {

    }

    @Override
    public void windowDeiconified(WindowEvent e) {

    }

    @Override
    public void windowActivated(WindowEvent e) {

    }

    @Override
    public void windowDeactivated(WindowEvent e) {

    }
}